package jpavlich.cardsorter;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.EventQueue;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.geom.Point2D;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenuBar;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import fr.inria.zvtm.animation.Animation;
import fr.inria.zvtm.animation.interpolation.IdentityInterpolator;
import fr.inria.zvtm.engine.View;
import fr.inria.zvtm.engine.VirtualSpaceManager;
import javax.swing.JLabel;
import javax.swing.JSeparator;
import net.miginfocom.swing.MigLayout;
import java.awt.FlowLayout;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Insets;

public class CardSorterWindow {

	private JFrame frmExperiment;
	private CardSorter cardSorter;

	/**
	 * Create the application.
	 */
	public CardSorterWindow(CardSorter cardSorter, boolean admin) {
		this.cardSorter = cardSorter;
		initialize(admin);

	}

	public void open() {
		frmExperiment.setVisible(true);
	}

	/**
	 * Initialize the contents of the frame.
	 * 
	 * @param cardSorter
	 * @param admin
	 */
	private void initialize(boolean admin) {
		frmExperiment = new JFrame();
		frmExperiment.setTitle("Experimento: " + cardSorter.getExperimentID());
		frmExperiment.setBounds(100, 100, 450, 300);
		frmExperiment.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

		// cardSorter = new CardSorter(View.STD_VIEW);

		Component panel = cardSorter.getView().getFrame();
		frmExperiment.getContentPane().add(panel, BorderLayout.CENTER);

		JPanel toolbarPanel = new JPanel();
		frmExperiment.getContentPane().add(toolbarPanel, BorderLayout.NORTH);
		GridBagLayout gbl_toolbarPanel = new GridBagLayout();
		gbl_toolbarPanel.columnWidths = new int[] { 230, 0, 0, 447, 310, 0 };
		gbl_toolbarPanel.rowHeights = new int[] { 61, 0 };
		gbl_toolbarPanel.columnWeights = new double[] { 1.0, 0.0, 0.0, 0.0,
				1.0, Double.MIN_VALUE };
		gbl_toolbarPanel.rowWeights = new double[] { 1.0, Double.MIN_VALUE };
		toolbarPanel.setLayout(gbl_toolbarPanel);

		JPanel experimentPanel = new JPanel();
		GridBagConstraints gbc_experimentPanel = new GridBagConstraints();
		gbc_experimentPanel.insets = new Insets(0, 0, 0, 5);
		gbc_experimentPanel.fill = GridBagConstraints.BOTH;
		gbc_experimentPanel.gridx = 1;
		gbc_experimentPanel.gridy = 0;
		toolbarPanel.add(experimentPanel, gbc_experimentPanel);
		experimentPanel.setLayout(new MigLayout("", "[][][]", "[]"));

		JButton btnMostrarTodo = new JButton("Mostrar todas las cartas");
		experimentPanel.add(btnMostrarTodo, "cell 0 0");

		final JButton btnComenzar = new JButton("Comenzar");
		experimentPanel.add(btnComenzar, "cell 1 0");
		final JButton btnTerminar = new JButton("Terminar");
		experimentPanel.add(btnTerminar, "cell 2 0");
		btnTerminar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Controller.closeCardSorterWindow(cardSorter, frmExperiment);
			}
		});
		
		frmExperiment.addWindowListener(new WindowAdapter() {
	        @Override public void windowClosing(WindowEvent e) {
	        	Controller.closeCardSorterWindow(cardSorter, frmExperiment);
	        }
	    });
		
		btnTerminar.setEnabled(false);
		btnComenzar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				cardSorter.record();
				btnComenzar.setEnabled(false);
				btnTerminar.setEnabled(true);
			}
		});
		// btnTerminar.setEnabled(false);
		btnMostrarTodo.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				cardSorter.zoomToFit();
			}
		});

		experimentPanel.setVisible(!admin);

		JPanel controlPanel = new JPanel();
		GridBagConstraints gbc_controlPanel = new GridBagConstraints();
		gbc_controlPanel.anchor = GridBagConstraints.NORTH;
		gbc_controlPanel.insets = new Insets(0, 0, 0, 5);
		gbc_controlPanel.gridx = 3;
		gbc_controlPanel.gridy = 0;
		toolbarPanel.add(controlPanel, gbc_controlPanel);
		controlPanel.setLayout(new MigLayout("", "[87px][75px]", "[29px]"));
		
				JButton btnPlay = new JButton("Play");
				controlPanel.add(btnPlay, "cell 0 0,alignx left,aligny top");
				btnPlay.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						cardSorter.play();
					}
				});
		
				JButton btnStop = new JButton("Stop");
				controlPanel.add(btnStop, "cell 1 0,alignx left,aligny top");
		btnStop.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				cardSorter.stop();
			}
		});

		controlPanel.setVisible(admin);

		frmExperiment.setExtendedState(Frame.MAXIMIZED_BOTH);
		// frmExperiment.setUndecorated(true);
		final ImageIcon[] icons = new ImageIcon[] {
				new ImageIcon(
						CardSorterWindow.class
								.getResource("/jpavlich/cardsorter/resources/record.png")),
				new ImageIcon(
						CardSorterWindow.class
								.getResource("/jpavlich/cardsorter/resources/stop.png")) };
	}

}
